package homework.gademo.interceptor;

import homework.gademo.common.SessionUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class LoginHandlerInterceptor implements HandlerInterceptor {
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		Object user = request.getSession().getAttribute(SessionUtil.SESSION_KEY_USER_ID);
		// 如果获取的request的session中的loginUser参数为空（未登录），就返回登录页，否则放行访问
		if (user == null) {
			// 未登录，给出错误信息，
			request.setAttribute("msg", "无权限请先登录");
			// 获取request返回页面到登录页
			response.sendRedirect(request.getContextPath()+"/");
			return false;
		} else {
			// 已登录，放行
			return true;
		}
	}

	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {

	}

	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex)
			throws Exception {

	}
}
